package com.peppa.petshop.controller;


import com.peppa.petshop.service.CartService;
import com.peppa.petshop.service.CategoryService;
import com.peppa.petshop.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.security.PrivateKey;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张三
 * @since 2021-06-02
 */
@Controller
@RequestMapping("/user")
public class CartController {
    @Autowired
    private CartService cartService;

    @RequestMapping("/addCart")
    @ResponseBody
    @CrossOrigin
    public String add(HttpServletRequest request, @RequestParam("goodId") Integer goodId){
//        HttpSession session = request.getSession();
//        String customerId1 = (String) session.getAttribute("customerId");
//        System.out.println(customerId1);
        Integer customerId = new Integer("2");
        String flag = cartService.add(goodId,customerId);
        return flag;
    }
    @RequestMapping("/cart")
    @ResponseBody
    @CrossOrigin
    public List<CartVO> cartList(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("我来看我的购物车了");
//        HttpSession session = request.getSession();
//        Cookie[] cookies = request.getCookies();
//        String token = "";
//        for (Cookie cookie : cookies) {
//            switch(cookie.getName()){
//                case "customerId":
//                    token = cookie.getValue();
//                    break;
//                default:
//                    break;
//            }
//        }
//        session.getAttribute("customerId");
//        System.out.println("token:"+token);
//        Integer customerId = (Integer) session.getAttribute("customerId");
//        Integer customerId = new Integer(token);
        Integer customerId = new Integer("2");
        System.out.println("用户customerId:"+customerId);
        List<CartVO> cartList = cartService.cartList(customerId);
        response.setHeader("Access-Control-Allow-Credentials", "true");
        return cartList;
    }

}

